package com.startup;

import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
//import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import com.im.server.general.ServerInfo;
import com.im.task.MessageConsumer;

/**
 * @Author: XiaHui
 * @Date: 2015年12月17日
 * @ModifyUser: XiaHui
 * @ModifyDate: 2015年12月17日
 */
@Configuration
// @PropertySource({ "classpath:config/rabbit.properties" })
@PropertySource({ "file:config/setting/mq.properties" })
public class MessageQueueConfig {

	@Autowired
	private Environment environment;

	@Bean
	public ServerInfo serverInfo() {
		ServerInfo bean = new ServerInfo();
		return bean;
	}

	// //////////////////////////////////////////
	@Bean
	public ConnectionFactory rabbitConnectionFactory() {
		CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
		connectionFactory.setHost(environment.getProperty("rabbit.conn.hostname"));
		connectionFactory.setPort(environment.getProperty("rabbit.conn.port", Integer.class));
		connectionFactory.setVirtualHost(environment.getProperty("rabbit.conn.virtual.host"));
		connectionFactory.setUsername(environment.getProperty("rabbit.conn.username"));
		connectionFactory.setPassword(environment.getProperty("rabbit.conn.password"));
		connectionFactory.setChannelCacheSize(environment.getProperty("rabbit.channel.cache.size", Integer.class));
		// connectionFactory.setUsername("only");
		// connectionFactory.setPassword("123456");

		// 声明交换器并绑定队列
		/** 分发模式 **/
		// TopicExchange logExchange = new TopicExchange("trade.log.exchange",
		// false, true);
		/**
		 * 广播模式
		 */
		// FanoutExchange registerFanoutExchange = new
		// FanoutExchange("user.register.exchange", true, true);
		return connectionFactory;
	}

	/////////////////////////////////////////

	@Bean
	public Queue messageQueue() {
		ServerInfo info = serverInfo();
		Queue queue = new Queue(info.getId(), true, false, true);
		return queue;
	}

	@Bean
	public TopicExchange messageTopicExchange() {
		TopicExchange exchange = new TopicExchange("oim.server.message.exchange", false, true);
		return exchange;
	}

	@Bean
	public RabbitAdmin messageRabbitAdmin() {
		ServerInfo info = serverInfo();
		RabbitAdmin rabbitAdmin = new RabbitAdmin(rabbitConnectionFactory());
		rabbitAdmin.declareQueue(messageQueue());
		rabbitAdmin.declareExchange(messageTopicExchange());
		rabbitAdmin.declareBinding(BindingBuilder.bind(messageQueue()).to(messageTopicExchange()).with(info.getId()));
		return rabbitAdmin;
	}

	@Bean
	public SimpleMessageListenerContainer userEventSimpleMessageListenerContainer() {
		SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(rabbitConnectionFactory());
		container.setMessageListener(messageListenerAdapter());
		container.addQueues(messageQueue());
		container.start();
		return container;
	}

	@Bean
	public MessageListenerAdapter messageListenerAdapter() {
		MessageListenerAdapter adapter = new MessageListenerAdapter(messageConsumer());
		return adapter;
	}

	@Bean
	public MessageListener messageConsumer() {
		MessageConsumer consumer = (new MessageConsumer());
		return consumer;
	}

//	@Bean
//	public ChannelAwareMessageListener messageConsumer() {
//		MessageConsumer consumer = (new MessageConsumer());
//		return consumer;
//	}
	@Bean
	public RabbitTemplate messageRabbitTemplate() {
		RabbitTemplate bean = new RabbitTemplate();
		bean.setConnectionFactory(rabbitConnectionFactory());
//		bean.setExchange("oim.server.message.exchange");
//		bean.setRoutingKey("oim.server.message.exchange");
		return bean;
	}
}
